package com.github.llyb120.logger.java;

import java.util.ArrayList;
import java.util.List;
import java.util.StringJoiner;

/**
 * @Author: Administrator
 * @Date: 7/27/2021 11:53 AM
 */
public class JavaLogger {

//    private static JavaLogger logger = new JavaLogger();
//    private JavaLogger(){ }
//    public static JavaLogger getInstance(){
//        return logger;
//    }
    List<LoggerEntity> loggerEntities = new ArrayList<>();

    public List<LoggerEntity> getLoggerEntities() {
        return loggerEntities;
    }

    List<String> logs = new ArrayList<>();

    public List<String> getLogs() {
        return logs;
    }

    public void log(String str, Object... objects) {
        logs.add(String.format(str, objects));
    }


    public void _show(int no, String str) {
        String result = binaryToHexString(str.getBytes());
        LoggerEntity entity = LoggerEntity.builder()
            .lineNo(no)
            .logs(new ArrayList<>(logs))
            .result(result)
            .build();
        loggerEntities.add(entity);
    }

    private static String hexStr = "0123456789ABCDEF"; //全局

    public static String binaryToHexString(byte[] bytes) {
        StringJoiner sj = new StringJoiner(" ");
        String hex = "";
        for (int i = 0; i < bytes.length; i++) {
            //字节高4位
            hex = String.valueOf(hexStr.charAt((bytes[i] & 0xF0) >> 4));
            //字节低4位
            hex += String.valueOf(hexStr.charAt(bytes[i] & 0x0F));
            sj.add(hex);
        }
        return sj.toString();

    }

}
